# Contributor: Hani Shawa <hani.shawa@ev-box.com>
# Maintainer: Hani Shawa <hani.shawa@ev-box.com>
pkgname=lua-lanes
_rockname=${pkgname#lua-}
pkgver=3.13.0
pkgrel=0
pkgdesc="portable, message passing multithreading library"
url="https://github.com/LuaLanes/lanes"
license="MIT"
arch="noarch"
makedepends="luarocks"
options="!check" # Test dependencies not available
source="$_rockname-$pkgver.tar.gz::https://github.com/LuaLanes/lanes/archive/v$pkgver.tar.gz"
builddir="$srcdir/$_rockname-$pkgver"
_luaversions="5.1 5.2 5.3"

for _luaversion in $_luaversions; do
	makedepends="$makedepends lua$_luaversion-dev"
	subpackages="$subpackages lua$_luaversion-$_rockname:_subpackage"
done

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		luarocks-$lver \
		               CFLAGS="-fPIC -DPTHREAD_MUTEX_RECURSIVE_NP=PTHREAD_MUTEX_RECURSIVE -Dpthread_yield=sched_yield"  \
		               LUA_INCDIR="$(pkg-config --variable=includedir lua$lver)" \
		               make --tree="./build" \
		               "$_rockname-$pkgver-"*".rockspec"

	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"

	local path; for path in "lib/luarocks/rocks-$lver" "share/lua/$lver"; do
		mkdir -p "$subpkgdir/usr/${path%/*}"
		mv "$builddir/build/$path" "$subpkgdir/usr/$path/"
	done
}

sha512sums="bb509181757fa2b4f07a55962015b35ca6bb867b18ec89dd655f333babe2e12f56479fa85bc98e0dacf5bec2a523265bbc3a978396dc332dc1fdda4df760b2e5  lanes-3.13.0.tar.gz"
